package com.yaozheng.vocationaltraining.service.impl;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IMasterView;
import com.yaozheng.vocationaltraining.service.MasterService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 闯关之路服务器访问
 */
@EBean
public class MasterServiceImpl implements MasterService {
    IMasterView iMasterView;

    @Override
    public void init(IMasterView iMasterView) {
        this.iMasterView = iMasterView;
    }

    @Background
    @Override
    public void masterProgress() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/master/all",
                    iMasterView.getToken(), new BaseView(iMasterView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iMasterView.masterProgressSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iMasterView.masterProgressError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iMasterView.isResponseResult()) {
                iMasterView
                        .masterProgressError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
